package foroweb.controllers;

import foroweb.ejb.Busquedas;
import foroweb.entidades.*;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.*;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

@ManagedBean
@RequestScoped
public class ComprobarRol implements Serializable{

    //Atributos
    private User u;
    @EJB
    private Busquedas buscar;

    //Constructores
    public ComprobarRol(){
        u=(User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    //Metodos
    public boolean checkRole(String rol){
        return u.getAuthorities().iterator().next().getAuthority().equals(rol);
    }
    public boolean checkUser(String nick){
        if(checkRole("Administrador")){
            return true;
        }else{
            return u.getUsername().equals(nick);
        }
    }
    public boolean checkMod(Seccion s){
        if(s.getModeradores().contains(buscar.buscarUsuario(u.getUsername()))){
            return true;
        }else{
            return false;
        }
    }
}
